# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=alembic
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.8.5
pkgrel=2
pkgdesc="A open framework for storing and sharing scene data (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.alembic.io/"
msys2_repository_url="https://github.com/alembic/alembic"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-imath"
         "${MINGW_PACKAGE_PREFIX}-hdf5")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
             #"${MINGW_PACKAGE_PREFIX}-boost"
             #"${MINGW_PACKAGE_PREFIX}-python")
#optdepends=("${MINGW_PACKAGE_PREFIX}-boost: Python bindings"
#            "${MINGW_PACKAGE_PREFIX}-python: Python bindings")
source=("https://github.com/alembic/${_realname}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-alembic-1.7.9-fix-windows-build.patch"
        "0002-alembic-1.7.9-remove-msc_ver-and-fix-builtins-and-includes.patch")
sha256sums=('180a12f08d391cd89f021f279dbe3b5423b1db751a9898540c8059a45825c2e9'
            '81df673984894ec38b8a24aeb8d3f17a02b3aa697c2c943970b8984024ab9a64'
            '639eec0611f3f40a74003e58946f556e6e4e0a54c85f10bcda8c3da1b1de6a81')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -Np1 -i "${srcdir}/0001-alembic-1.7.9-fix-windows-build.patch"
  patch -Np1 -i "${srcdir}/0002-alembic-1.7.9-remove-msc_ver-and-fix-builtins-and-includes.patch"
}

build() {
  # Static Build
  mkdir -p "${srcdir}"/build-${MSYSTEM}-static && cd "${srcdir}"/build-${MSYSTEM}-static

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  export OPENEXR_ROOT=${MINGW_PREFIX}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DUSE_HDF5=ON \
      -DUSE_STATIC_HDF5=ON \
      -DUSE_BINARIES=OFF \
      -DUSE_TESTS=OFF \
      -DALEMBIC_ILMBASE_LINK_STATIC=OFF \
      -DALEMBIC_SHARED_LIBS=OFF \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  # Shared Build
  mkdir -p "${srcdir}"/build-${MSYSTEM}-shared && cd "${srcdir}"/build-${MSYSTEM}-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DUSE_HDF5=ON \
      -DUSE_PYALEMBIC=OFF \
      -DUSE_TESTS=OFF \
      -DALEMBIC_SHARED_LIBS=ON \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  # Static Install
  cd "${srcdir}"/build-${MSYSTEM}-static
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  # Shared Install
  cd "${srcdir}"/build-${MSYSTEM}-shared
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  local WINPREFIX=$(cygpath -wm ${MINGW_PREFIX})
  # Fix paths
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f -name '*.cmake' \
    -exec sed -i -e "s|${WINPREFIX}|\$\{_IMPORT_PREFIX\}|g" {} \;

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
